package com.erp.stock.service.impl;

import com.erp.common.util.ServiceUtil;
import com.erp.common.util.StringUtil;
import com.erp.produce.vo.RawMaterialInventoryVO;
import com.erp.stock.dao.CooperativesRawMaterialInventoryDao;
import com.erp.stock.dao.RawMaterialInventoryDao;
import com.erp.stock.entity.CooperativesRawMaterialInventory;
import com.erp.stock.entity.RawMaterialInventory;
import com.erp.stock.service.ProcessedProductionStockService;
import com.erp.stock.service.RawMaterialInventoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RawMaterialInventoryServiceImpl implements RawMaterialInventoryService {

    @Autowired
    RawMaterialInventoryDao dao;

    @Autowired
    CooperativesRawMaterialInventoryDao cooperativesRawMaterialInventoryDao;

    @Autowired
    ProcessedProductionStockService processedProductionStockService;

    @Override
    public Boolean update(RawMaterialInventory rawMaterialInventory) {
        RawMaterialInventory rawMaterialInventory1 = dao.findById(rawMaterialInventory.getId()).get();
        BeanUtils.copyProperties(rawMaterialInventory,rawMaterialInventory1);
        return dao.saveAndFlush(rawMaterialInventory1) != null;
    }

    @Override
    public Boolean add(RawMaterialInventory rawMaterialInventory) {
        return dao.save(rawMaterialInventory) != null;
    }

    @Override
    public Boolean deleteIds(Long[] ids) {
        return ServiceUtil.deleteIds(ids,dao);
    }

    @Override
    public List<RawMaterialInventory> listPage(int page, int size) {
        return dao.findAll(PageRequest.of(page, size,new Sort(Sort.Direction.ASC,"id"))).getContent();
    }

    @Override
    public List<RawMaterialInventory> listPageLike(int page, int size, String column) {
        return dao.findAllByRawMaterialIdLike(StringUtil.addLike(column),PageRequest.of(page, size));
    }

    @Transactional
    @Override
    public Boolean saveList(RawMaterialInventoryVO [] list) {
        if (list.length > 0) {
            Long orderId = list[0].getOrderId();
            try {
                for (RawMaterialInventoryVO rawMaterialInventoryVO : list) {
                    //返回到原库存
                    if (rawMaterialInventoryVO.getReturnAddress().equals("self")) {
                        List<RawMaterialInventory> allByRawMaterialId = dao.findAllByRawMaterialId(rawMaterialInventoryVO.getId());
                        if (allByRawMaterialId.size() == 0){
                            RawMaterialInventory rawMaterialInventory = new RawMaterialInventory();
                            rawMaterialInventory.setRawMaterialId(rawMaterialInventoryVO.getId());
                            rawMaterialInventory.setCount(rawMaterialInventoryVO.getReturnRMCount());
                            dao.save(rawMaterialInventory);
                        } else{
                            RawMaterialInventory rawMaterialInventory = allByRawMaterialId.get(0);
                            rawMaterialInventory.setCount(rawMaterialInventory.getCount()+rawMaterialInventoryVO.getReturnRMCount());
                            dao.saveAndFlush(rawMaterialInventory);
                        }
                        CooperativesRawMaterialInventory cooInventory =
                                cooperativesRawMaterialInventoryDao.findFirstByCooperativesNameAndRawMaterialIdAndOrderId(rawMaterialInventoryVO.getCooperativesName(), rawMaterialInventoryVO.getId(), orderId);
                        cooInventory.setRestInventory(0.0);
                        cooperativesRawMaterialInventoryDao.saveAndFlush(cooInventory);
                    } else {
                        //返回到外协企业库存
                        CooperativesRawMaterialInventory cooperativesRawMaterialInventory =
                                cooperativesRawMaterialInventoryDao.findFirstByCooperativesNameAndRawMaterialIdAndOrderId(rawMaterialInventoryVO.getCooperativesName(),rawMaterialInventoryVO.getId(),orderId);
                        cooperativesRawMaterialInventory.setRestInventory(rawMaterialInventoryVO.getReturnRMCount());
                        cooperativesRawMaterialInventoryDao.saveAndFlush(cooperativesRawMaterialInventory);
                    }

                }
                Boolean success = processedProductionStockService.insertStocksByOrder(orderId);
                if (!success) {
                    return false;
                }
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }
}
